﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using DataContracts;

namespace TrainingWorkshop.MVPWebsite.User.Views
{
	public partial class UserList : Microsoft.Practices.CompositeWeb.Web.UI.Page, IUserListView
	{
		private UserListPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public UserListPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        /// <summary>
        /// Implement Deleting User
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UserListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int UserId = Convert.ToInt32((UserListGridView.Rows[e.RowIndex].FindControl("UserIdlabel") as Label).Text);
            this._presenter.OnDeleteUser(UserId);     
        }

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//
	
        protected void UserListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton delete = e.Row.FindControl("DeleteLinkButton") as LinkButton;

                if (delete.Text == "Delete")
                    delete.Attributes.Add("onclick", "return confirm('Do you want to delete it?')");
            }
        }


        protected void UserListGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int UserId = Convert.ToInt32((UserListGridView.Rows[e.NewEditIndex].FindControl("UserIdlabel") as Label).Text);
            Response.Redirect("UpdateUser.aspx?UserId=" + UserId);
        }

        protected void CreateNewUserButton_Click(object sender, EventArgs e)
        {
            this._presenter.OnCreateUser();
        }

        /// <summary>
        /// Search User by UserName and Department
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this._presenter.OnSearchUser(DepartmentId, FirstName, LastName);
        }

        public string FirstName
        {
            get { return this.FirstNameTextBox.Text; }
        }
        public string LastName
        {
            get { return this.LastNameTextBox.Text; }
        }
        public int DepartmentId
        {
            get { return Convert.ToInt32(this.DepartmentDropDownList.SelectedValue); }
        }
       
        public void SkipToAnotherPage(string address)
        {
            Response.Redirect(address);
 
        }
        public void ShowOperationMessage(string message)
        {
            //Response.Write("<script>alert('" + message + "');</script>");
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }
        public List<UserDTO> Users
        {
            set
            {
                this.UserListGridView.DataSource = value;
                UserListGridView.DataBind();
            }
        }
        public List<DepartmentDTO> Departments 
        { 
            set
            {
                this.DepartmentDropDownList.DataSource = value;
                DepartmentDropDownList.DataTextField = "DepartmentName";
                DepartmentDropDownList.DataValueField = "DepartmentId";
                DepartmentDropDownList.DataBind();
                DepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
            } 
        }
}
}

